package com.dopper.ws.example;


import com.dopper.ws.AbstractWebsocket;
import com.dopper.ws.annotation.WebsocketMapping;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * description:
 *
 * @author jincarry
 * date: 2019-07-10 8:55
 */
@WebsocketMapping("/home")
public class HomeWebsocket extends AbstractWebsocket {
    private final static Logger log = LoggerFactory.getLogger(HomeWebsocket.class);

    @Override
    public void onOpen(Channel channel, Map<String, String> headers, Map<String, List<String>> queryString) {
        log.info("ws on open : channelId=[{}] headers=[{}] params=[{}]", channel.id(), headers, queryString);
        if (queryString == null) {
            rejectConnection(channel);
        }
    }

    @Override
    public void onMsg(Channel channel, String content) {
        log.info("receive: [{}] ", content);
        sendMsg(channel, content);
    }

    @Override
    public void onMsg(Channel channel, byte[] bytes) {
        log.info("receive: [{}]", bytes);
        sendMsg(channel, bytes);
    }

    @Override
    public void onClose(Channel channel) {
        log.info("ws on close: channelId=[{}]", channel.id());
    }

    @Override
    public void onError(Channel channel, Throwable throwable) {
        log.info("ws on error: channelId=[{}]", channel.id(), throwable);
    }
}
